package com.linkstec.bookshop.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.linkstec.bookshop.mapper.CommentMapper;
import com.linkstec.bookshop.pojo.Comment;
import com.linkstec.bookshop.service.CommentService;
import com.linkstec.bookshop.util.PageBean;
import com.linkstec.bookshop.util.ToolUtil;
import com.linkstec.bookshop.vo.CommentVo;
import com.linkstec.bookshop.vo.GoodsVo;

@Service
public class CommentServiceImpl implements CommentService {

	@Autowired
	private CommentMapper commentMapper;

	@Override
	public PageBean<CommentVo> findAllComment(int page) throws RuntimeException {

		PageBean<CommentVo> goodsBean = new PageBean<CommentVo>();

		goodsBean.setPage(page);// 第几页

		int limitPage = 10;
		goodsBean.setLimitPage(limitPage);

		int count = commentMapper.selCommentCount();// 查询数量
		int totlePage = 0;

		if (Math.ceil(count % limitPage) == 0) {
			totlePage = count / limitPage;
		} else {
			totlePage = count / limitPage + 1;
		}
		goodsBean.setTotlePage(totlePage);// 设置总页数

		int beginPage = (page - 1) * limitPage;// 开始

		List<CommentVo> list = commentMapper.findComment(beginPage, limitPage);

		goodsBean.setList(list);
		return goodsBean;
	}

	@Transactional
	@Override
	public void delete(int cmId) throws RuntimeException {
		// TODO Auto-generated method stub
		commentMapper.delete(cmId);
	}

	@Transactional
	@Override
	public void addComment(Comment comment) throws RuntimeException {
		// TODO Auto-generated method stub
		comment.setComTime(ToolUtil.getCurrentTime());
		commentMapper.addComment(comment);
	}

}
